package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.logging.log4j.message.Message;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path="/member")
@CrossOrigin
public class MemberController {

    @Reference
    private MemberService memberService;
    /**
     * 新增会员
     *
     * @param member
     * @return
     */
    @PostMapping(path = "/add")
    public Result add(@RequestBody Member member) {
        try {
             memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    /**
     * 分页查询会员数据
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping(path = "/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        //分页查询检查组信息
        try {
            PageResult pageResult = memberService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }



    /**
     * 通过查询id
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable int id) {
        try {
            //查询检查项
            Member member = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    /**
     *
     * 禁用设置
     * @param id
     * @param status
     * @return
     */
    @PutMapping(path = "/{id}/{status}")
    public Result status(@PathVariable Integer id,@PathVariable Integer status){
        try {
            memberService.updateStatus(id,status);

            return new Result(true, MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
        }
    }





    /**
     * 确定编辑, 更新数据
     *
     * @param member
     * @return
     */
    @PutMapping(path = "/update")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.edit(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 查询所有会员
     *
     * @return
     */
    @GetMapping(path = "/findAll")
    public Result findAll() {
        try {
            List<Member> memberList = memberService.findAll();
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, memberList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
}
